package com.lipeng.api;

import com.lipeng.bean.Attention;
import com.lipeng.bean.Club;
import com.lipeng.service.ClubService;
import com.lipeng.utils.AjaxResult;

import com.lipeng.utils.IPUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by dllo on 18/3/16.
 */
@Controller
@ResponseBody
@RequestMapping(value = "/api/")
public class ClubControllerAPI {
    @Resource
    private ClubService clubService;


    //俱乐部列表
    @RequestMapping(value = "allclub")
    public AjaxResult allClub(){
        AjaxResult result = new AjaxResult();
        List<Club> clubList = clubService.selectAllClub();
        if (clubList == null){
            result.setCode("0");
            result.setMessage("查询失败");
            return result;
        }
        for (Club club : clubList){
            Integer count = clubService.selectAttentionCountByCid(club.getCid());
            club.setCpic(IPUtils.getIP()+":8080"+club.getCpic());
            club.setAttentionCount(count);
        }
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(clubList);
        return result;
    }

    //俱乐部详情
    @RequestMapping(value = "clubdetail")
    public AjaxResult clubDetail(@RequestParam("cid")Integer cid,@RequestParam("uid")Integer uid){
        AjaxResult result = new AjaxResult();
        Club club = clubService.selectClubById(cid);
        if (club == null){
            result.setCode("0");
            result.setMessage("查询失败");
            return result;
        }
        boolean isAttention = true;
        Attention attention = clubService.selectClubAttention(cid,uid);
        if (attention==null){
            isAttention = false;
        }
        club.setIsAttention(isAttention);
        club.setCpic(IPUtils.getIP()+":8080"+club.getCpic());
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(club);
        return result;
    }



    //关注俱乐部
    @RequestMapping(value = "addattention")
    public AjaxResult addAttention(@RequestParam("uid")Integer uid,@RequestParam("cid") Integer cid){
        return clubService.addAttention(uid,cid);
    }

    //取消关注
    @RequestMapping(value = "delattention")
    public AjaxResult delAttention(@RequestParam("attid")Integer attid){
        return clubService.delAttention(attid);
    }

    //根据俱乐部名称搜索俱乐部
    @RequestMapping(value = "searchclubname")
    public AjaxResult searchname(@Param("name")String name){
        AjaxResult result = new AjaxResult();
        List<Club> clubList = clubService.selectClubByName(name);
        if (clubList==null){
            result.setCode("0");
            result.setMessage("无此商品");
            return result;
        }
        for (Club club : clubList){
            Integer count = clubService.selectAttentionCountByCid(club.getCid());
            club.setCpic(IPUtils.getIP()+":8080"+club.getCpic());
            club.setAttentionCount(count);
        }
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(clubList);
        return result;
    }
}
